package com.android.gufengmall.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.AddressBean;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {

    private Context mContext;
    private List<AddressBean> mAddresses;
    private IDeleteLongClick mIDeleteLongClick;

    public AddressAdapter(Context context, List<AddressBean> addresses) {
        mContext = context;
        mAddresses = addresses;
    }

    @NonNull
    @Override
    public AddressAdapter.AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AddressViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_address,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AddressAdapter.AddressViewHolder holder, int position) {
        AddressBean address = mAddresses.get(position);
        holder.mTvAddress.setText(address.getAddress());
        holder.itemView.setOnLongClickListener(v -> {
            mIDeleteLongClick.onLongClickDelete(position);
            return false;
        });
    }

    @Override
    public int getItemCount() {
        return mAddresses.size();
    }

    public class AddressViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvAddress;

        public AddressViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvAddress = itemView.findViewById(R.id.tv_address);
        }
    }

    public void setIDeleteLongClick(IDeleteLongClick longClick) {
        mIDeleteLongClick = longClick;
    }

    public interface IDeleteLongClick{
        void onLongClickDelete(int position);
    }
}
